package com.situ.crm.controller.back;

import javax.servlet.http.HttpSession;
import javax.swing.SortingFocusTraversalPolicy;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.situ.crm.common.ServerResponse;
import com.situ.crm.entity.Role;
import com.situ.crm.entity.User;
import com.situ.crm.service.IUserService;

@Controller
@RequestMapping("/user")
public class UserController {

	private static final String Integer = null;
	@Autowired
	private IUserService userService;

	// 跳转到界面
	@RequestMapping("/getPage")
	public String getPage() {

		return "/user/user";
	}

	// 分页展示
	@RequestMapping("/pageList")
	@ResponseBody
	public ServerResponse pageList(Integer page, Integer limit) {
		ServerResponse serverResponse = userService.pageList(page, limit);
		System.out.println(serverResponse);
		return serverResponse;
	}

	// 跳转到添加界面
	@RequestMapping("/add")
	public String add() {

		return "/user/user_add";
	}

	// 添加功能
	@RequestMapping("/insert")
	@ResponseBody
	public ServerResponse insert(User user, String roles) {
		System.out.println(user);
		System.out.println(roles);
		ServerResponse serverResponse = userService.insert(user, roles);
		return serverResponse;
	}

	// 根据ID删除一行
	@RequestMapping("/deleteById")
	@ResponseBody
	public ServerResponse deleteById(Integer id) {
		ServerResponse serverResponse = userService.deleteById(id);
		return serverResponse;
	}

	// 跳转到修改界面
	// 携带了ID过来，先通过ID查找user信息，然后再跳转
	@RequestMapping("/toupdate")
	public String toupdate(Integer id, Model model) {
		User user = userService.selectById(id);
		model.addAttribute("user", user);

		return "/user/user_update";
	}

	// 修改界面的角色展示
	@RequestMapping("/selectUserAndRoles")
	@ResponseBody
	public ServerResponse selectUserAndRoles(Integer userId) {
		System.out.println("userId = " + userId);
		return userService.selectUserAndRoles(userId);
	}

	// 修改功能
	@RequestMapping("/update")
	@ResponseBody
	public ServerResponse update(User user, String roles) {
		System.out.println(user);
		System.out.println(roles);
		ServerResponse serverResponse = userService.update(user, roles);

		return serverResponse;
	}

	// 查询创建人
	@RequestMapping("/selectCreateMan")
	@ResponseBody
	public ServerResponse selectCreateMan() {
		ServerResponse serverResponse = userService.selectCreateMan();

		return serverResponse;
	}
	//跳转到登录界面
	@RequestMapping("/login")
	public String login() {
	
		return "/login";
	}
	//登录
	@RequestMapping("/getLogin")
	@ResponseBody
	public ServerResponse getLogin(String username,String password,HttpSession session) {
		System.out.println(username+password);
		return userService.login(username,password,session);
	}

}
